<template>
	<div>
    <!--认证失败消息提示-->
    <div class="authentication-warning" v-if="type === 'update' || type === 'change'">
      {{ warningStud }}
    </div>
    <!--上传-->
    <div class="authentication-content">
      <!--学校验证组件set-->
      <v-student class="v-student-component" @change-params="getStudentInfo" v-if="type === 'set'"></v-student>
      <!--学校验证组件change || update-->
      <v-student class="v-student-component" @change-params="getStudentInfo" v-else-if="type === 'change' || type === 'update'" :parentData="studentInfo"></v-student>
    </div>
    <!--提交按钮-->
    <div class="btn-submit">
      <button type="button" :class="{'btn-forbid-item': !allowSubmitStud}" @click="onSubmit('/personal/edit/success')" :disabled="!allowSubmitStud" v-if="changeStatus === '2'">{{$t('message.PersonalIdentityAuthenticationSubmitBtn')}}</button>
      <button type="button" :class="{'btn-forbid-item': !allowSubmitStud}" @click="onSubmit(computeHref)" :disabled="!allowSubmitStud" v-else>{{$t('message.PersonalIdentityAuthenticationNextBtn')}}</button></div>
	</div>
</template>

<script>
import VStudent from '@/components/base/VStudent'

export default {
  components: {
    VStudent
  },
  data () {
    return {
      // 后台加载过来验证信息的id
      studentInfoID: '',
      // 学校验证信息
      studentInfo: {},
      // 是否允许提交
      allowSubmitStud: false,
      // 认证失败原因
      warningStud: this.$t('message.pleaseFillStudInfo'),
      /**
       * '000': 身份认证未上传，学校认证未上传，紧急联系人认证未上传
       * '111': 身份认证失败，学校认证失败，紧急联系人认证失败
       * '222': 身份认证成功，学校认证成功，紧急联系人认证成功
       */
      changeStatus: '1'
    }
  },
  computed: {
    // 当前页面编辑类型
    type () {
      return this.$route.params.type || 'undefined'
    },
    // 判断提交或下一步跳转的页面
    computeHref () {
      if (this.changeStatus[0] === '0') {
        return '/personal/edit/contacts/set'
      } else if (this.changeStatus[0] === '1') {
        return '/personal/edit/contacts/change'
      }
    }
  },
  methods: {
    // 提交验证
    onSubmit (href) {
      // 新提交身份认证set
      if (this.type === 'set') {
        // 获取需要上传的数据
        let studData = {
          student_front_side_url: this.studentInfo.photo,
          school_name: this.studentInfo.school,
          school_district: this.studentInfo.schoolDistrict,
          education: this.studentInfo.education,
          college: this.studentInfo.institute,
          major: this.studentInfo.major,
          grade: this.studentInfo.grade,
          studentID: this.studentInfo.studentID,
          // entry_date: this.studentInfo.enrolledDate.toLocaleDateString(),
          entry_date: this.studentInfo.enrolledDate,
          student_card_type: this.studentInfo.photoSource = this.studentInfo.photoSource === 'studentCard' ? 1 : this.studentInfo.photoSource === 'campusNetwork' ? 2 : -1,
          // student_card_start: this.studentInfo.startDate,
          // student_card_end: this.studentInfo.endDate.toLocaleDateString()
          student_card_end: this.studentInfo.endDate
        }
        this.setStud(studData, href)
      // 更新身份认证信息update || 更换身份认证信息change
      } else {
        let studentData = {
          user_verify: [{
            'id': this.studentInfoID,
            'file': this.studentInfo.photo
          }]
        }
        let data = studentData.user_verify[0]
        // 构造数据
        for (let key in this.studentInfo) {
          if (this.getStudKey(key) === 'education' || this.getStudKey(key) === 'school_district') {
            if (this.studentInfo[key] instanceof Object) {
              data[this.getStudKey(key)] = this.studentInfo[key].name
            } else {
              data[this.getStudKey(key)] = this.studentInfo[key]
            }
          } else {
            data[this.getStudKey(key)] = this.studentInfo[key]
          }
        }
        data.student_card_type = data.student_card_type === 'studentCard' ? 1
                             : data.student_card_type === 'campusNetwork' ? 2
                                                                          : -1
        // data.entry_date = typeof data.entry_date === 'object' ? data.entry_date.toLocaleDateString()
        //                                                       : data.entry_date
        // data.student_card_end = typeof data.student_card_end === 'object' ? data.student_card_end.toLocaleDateString()
        //                                                                   : data.student_card_end
        // delete data.student_card_start
        // 提交更新
        this.updateStudConfirm(studentData, href)
      }
    },
    // 获取更新认证信息update
    getUpdateData () {
      let t = window.localStorage.getItem('thaiPayMallLanguage')
      var languageType
      if (t) {
        if (t === 'cn') {
          languageType = 1
        } else if (t === 'en') {
          languageType = 3
        } else {
          languageType = 2
        }
      } else {
        languageType = 2
      }
      this.$ajax.getDatedIndentity(languageType).then(res => {
        if (res.code === 200) {
          res.data.forEach((item, index) => {
            if (item.type === 'credit_graducation') {
              this.studentInfo = {
                school: item.school_name,
                schoolDistrict: item.school_district,
                education: item.education,
                institute: item.college,
                major: item.major,
                grade: item.grade,
                studentID: item.studentID,
                enrolledDate: item.entry_date,
                photoSource: item.student_card_type === 1 ? 'studentCard' : item.student_card_type === 2 ? 'campusNetwork' : 'undefined',
                // startDate: !item.student_card_start ? '0000/00/00' : item.student_card_start,
                endDate: !item.student_card_end ? '0000/00/00' : item.student_card_end,
                photo: ''
              }
              this.studentInfoID = item.id
              this.warningIden = this.$t('message.theReason') + this.$t('message.personalIdentitySchoolInfoTip')
            } else {
              console.log('getDatedIndentity()数据错误')
            }
          })
          console.log('Get update data')
        } else {
          console.log(res.code, res.msg)
        }
      })
    },
    // 获取修改认证信息change
    getChangeData () {
      let t = window.localStorage.getItem('thaiPayMallLanguage')
      var languageType
      if (t) {
        if (t === 'cn') {
          languageType = 1
        } else if (t === 'en') {
          languageType = 3
        } else {
          languageType = 2
        }
      } else {
        languageType = 2
      }
      this.$ajax.getFailIndentity(languageType).then(res => {
        if (res.code === 200) {
          res.data.forEach((item, index) => {
            if (item.type === 'credit_graducation') {
              this.studentInfo = {
                school: item.school_name,
                schoolDistrict: item.school_district,
                education: item.education,
                institute: item.college,
                major: item.major,
                grade: item.grade,
                studentID: item.studentID,
                enrolledDate: item.entry_date,
                photoSource: item.student_card_type === 1 ? 'studentCard' : item.student_card_type === 2 ? 'campusNetwork' : 'undefined',
                // startDate: !item.student_card_start ? '0000/00/00' : item.student_card_start,
                endDate: !item.student_card_end ? '0000/00/00' : item.student_card_end,
                // photo: item.file
                photo: ''
              }
              this.studentInfoID = item.id
              this.warningStud = this.$t('message.educationFailedReason') + (!item.msg || item.msg === '' ? this.$t('message.none') : item.msg)
            } else {
              console.log('响应数据的type字段读取失败')
            }
          })
          console.log('Get change data')
        } else if (res.code === 301) {
          console.log('Sign in again')
        } else {
          console.log('Something is wrong')
        }
      })
    },
    // 从学生信息子组件获取数据
    getStudentInfo (data) {
      if (data === 'no') {
        this.allowSubmitStud = false
      } else {
        this.studentInfo = data
        this.allowSubmitStud = true
      }
    },
    // 将组件中定义的key转化为要传给后台的key
    getStudKey (key) {
      switch (key) {
        case 'endDate':
          return 'student_card_end'
        case 'startDate':
          return 'student_card_start'
        case 'education':
          return 'education'
        case 'enrolledDate':
          return 'entry_date'
        case 'grade':
          return 'grade'
        case 'institute':
          return 'college'
        case 'major':
          return 'major'
        case 'photo':
          return 'file'
        case 'photoSource':
          return 'student_card_type'
        case 'school':
          return 'school_name'
        case 'schoolDistrict':
          return 'school_district'
        case 'studentID':
          return 'studentID'
        default:
          return key
      }
    },
    // 请求 set 学校认证
    setStud (studentData, href) {
      this.$ajax.proveData(studentData).then(res => {
        if (res.code === 200) {
          console.log('上传成功')
          this.$router.push(href)
        } else {
          console.log('setStud is wrong')
        }
      })
    },
    // 请求 update 学校认证的方法
    updateStudConfirm (studentData, href) {
      this.$ajax.updateIdentity(studentData).then((res) => {
        // 请求现在执行完成
        if (res.code === 200) {
          console.log('上传学校认证信息成功')
          this.$router.push(href)
        } else {
          console.log(res.msg)
        }
      })
    },
    // 获取用户全部认证状态
    getAuthenStatus () {
      this.$ajax.getAuthenStatus().then(res => {
        if (res.code === 200) {
          this.changeStatus = `${res.data.con_status}`
        } else {
          console.log(res.code, res.msg)
        }
      })
    }
  },
  mounted () {
    this.getAuthenStatus()
    if (this.type === 'update') {
      this.getUpdateData()
    } else if (this.type === 'change') {
      this.getChangeData()
    } else {
      console.log('set')
    }
    window.scrollTo(0, 0)
  }
}
</script>

<style scoped>
.authentication-warning {
	width: 805px;
	height: 50px;
	margin: 40px auto 0;
	line-height: 50px;
	font-size: 14px;
	border: 1px solid #ff600a;
	padding-left: 55px;
	background: #fffdf4 url(../../../assets/images/icon_errorTips.png) no-repeat 23px center;
}

.authentication-content {
	margin-top: 60px;
}

.v-student-component {
  width: 861px;
  margin: 0 auto;
}

.authentication-tip {
	height: 20px;
	line-height: 20px;
	font-size: 14px;
	color: #ff600a;
	margin-top: 12px;
	padding-left: 24px;
	background: url(../../../assets/images/icon_errorTips.png) no-repeat left center;
}

.btn-submit {
	height: 48px;
  margin-top: 80px;
	text-align: center;
}

.btn-submit > button {
	width: 376px;
	height: 48px;
	border: none;
	background-color: #ff600a;
	color: #fff;
	border-radius: 2px;
	cursor: pointer;
}

.btn-forbid-item {
  background-color: #d7d7d7 !important;
  cursor: not-allowed !important;
}
</style>
